using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Config;
using net_work.Data.DTO.Appr;
using net_work.Data.Entity.Appr;
using net_work.Data.Enum.Appr;
using net_work.Data.VO.Appr;
using net_work.IRepository.Appr;

namespace net_work.Repository.Appr;

public class FlowNodeRepository(ApplicationDbContext dbContext)
    : Repository<FlowNode>(dbContext), IFlowNodeRepository
{
    public async Task<long> InsertAsync(FlowNodeAddRequest flowNodeAddRequest)
    {
        var newFlowNode = new FlowNode
        {
            Remark = flowNodeAddRequest.Remark,
            Name = flowNodeAddRequest.Name,
            ProcessingType = flowNodeAddRequest.ProcessingType,
            ProcessingId = flowNodeAddRequest.ProcessingId,
            SuccessType = flowNodeAddRequest.SuccessType,
            SuccessConfig = flowNodeAddRequest.SuccessConfig,
            SuccessNodeId = flowNodeAddRequest.SuccessNodeId,
            FailureNodeId = flowNodeAddRequest.FailureNodeId,
            FlowId = flowNodeAddRequest.FlowId
        };

        await dbContext.FlowNodes.AddAsync(newFlowNode);
        await dbContext.SaveChangesAsync();
        return newFlowNode.Id;
    }

    public async Task<bool> UpdateAsync(FlowNodeUpdateRequest flowNodeUpdateRequest)
    {
        var flowNode = await dbContext.FlowNodes.FindAsync(flowNodeUpdateRequest.Id);
        if (flowNode == null)
        {
            return false;
        }

        flowNode.Remark = flowNodeUpdateRequest.Remark;
        flowNode.Name = flowNodeUpdateRequest.Name;
        flowNode.ProcessingType = flowNodeUpdateRequest.ProcessingType;
        flowNode.ProcessingId = flowNodeUpdateRequest.ProcessingId;
        flowNode.SuccessType = flowNodeUpdateRequest.SuccessType;
        flowNode.SuccessConfig = flowNodeUpdateRequest.SuccessConfig;
        flowNode.SuccessNodeId = flowNodeUpdateRequest.SuccessNodeId;
        flowNode.FailureNodeId = flowNodeUpdateRequest.FailureNodeId;

        await dbContext.SaveChangesAsync();
        return true;
    }

    public Task<PagedResult<FlowNodeResult>> GetFlowNodePagedAsync(PagedRequest request)
    {
        var query = from flowNode in dbContext.FlowNodes
            let objName =
                (flowNode.ProcessingType == NodeProcessingType.Dept)
                    ? dbContext.Departments.Where(c => c.Id == flowNode.ProcessingId).Select(c => c.DepName)
                        .FirstOrDefault()
                    : (flowNode.ProcessingType == NodeProcessingType.Teacher)
                        ? (from teacher in dbContext.Teachers
                            join people in dbContext.People on teacher.PsnId equals people.Id
                            where teacher.Id == flowNode.ProcessingId
                            select people.PsnName).FirstOrDefault()
                        : null // 默认情况，如果没有匹配的类型
            select new FlowNodeResult
            {
                Id = flowNode.Id,
                Name = flowNode.Name,
                ProcessingType = flowNode.ProcessingType,
                ProcessingId = flowNode.ProcessingId,
                ProcessingName = objName,
                SuccessType = flowNode.SuccessType,
                SuccessConfig = flowNode.SuccessConfig,
                SuccessNodeId = flowNode.SuccessNodeId,
                FailureNodeId = flowNode.FailureNodeId,
                FlowId = flowNode.FlowId,
                Remark = flowNode.Remark,
                CreatedBy = flowNode.CreatedBy,
                CreatedTime = flowNode.CreatedTime,
                UpdatedBy = flowNode.UpdatedBy,
                UpdatedTime = flowNode.UpdatedTime,
                DelFlag = flowNode.DelFlag
            };

        return GetPagedQueryResultAsync(query, request);
    }
}